//
//  General.m
//  GXWN
//
//  Created by iMacOne on 15/11/5.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "General.h"
#import "RootViewController.h"
#import "WebUtil.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface General()
@property (nonatomic, strong)MKMapView * mapView;




@end

@implementation General

//单例模式
 + (General *)shareGeneral
{
    static  General * general;
    @synchronized(self) {
        if (!general) {
            general = [[General alloc] init];
        }
        return general;
    }
}
+ (void)initialization:(UIViewController *)control isPoint:(BOOL)isPoint
{
    [General shareGeneral].control = control;
    
    [General shareGeneral].project_name = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey];
    NSDictionary * infoDic = [[NSBundle mainBundle] infoDictionary];
    [General shareGeneral].version = [infoDic objectForKey:@"CFBundleShortVersionString"];
//    [General shareGeneral].point = [NSMutableArray array];
    if (isPoint) {
        [General startPoint];
        
    }
    [General shareGeneral].versionTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:[General shareGeneral] selector:@selector(versionTimerThread) userInfo:nil repeats:NO];
    
}
#pragma mark 当定位发生改变，要处理的事情
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [General shareGeneral].point = [NSMutableArray array];

    if (locations != nil && locations.count > 0) {
        CLLocation * location = locations[0];
        [[General shareGeneral].point removeAllObjects];
        [[General shareGeneral].point addObject:[NSNumber numberWithDouble:location.coordinate.longitude]];
        [[General shareGeneral].point addObject:[NSNumber numberWithDouble:location.coordinate.latitude]];
        [[General shareGeneral].point addObject:[NSNumber numberWithDouble:location.altitude]];
        NSLog(@"haha%f",location.coordinate.latitude);
        NSLog(@"haha%f",location.coordinate.longitude);
//            MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 2000, 2000);
//            MKCoordinateRegion adjustedRegion = [[General shareGeneral].mapView regionThatFits:viewRegion];
//            [[General shareGeneral].mapView setRegion:adjustedRegion animated:YES];
        
        
        [[General shareGeneral] pointRegion];
        
        [General stopPoint];
//        CLGeocoder * geocoder = [[CLGeocoder alloc]];
        
        
    }
    
}

// 获取手机所在城市
- (void)pointRegion
{
    
}
// 5秒后仍然不定位，也要检测版本了
- (void)versionTimerThread {
    [[General shareGeneral].versionTimer invalidate];
    [General shareGeneral].versionTimer = nil;
    [[General shareGeneral] checkVersion];
}
// 通知服务器统计装机量和检测版本
- (void)checkVersion {
    if (![WebUtil isNet]) {
        return;
    }
//    NSString *clientCode = [ToolUtil md5:[[UIDevice currentDevice].identifierForVendor UUIDString]];
//    id lon = ([Constant bean].point.count > 1 ? [Constant bean].point[0] : @"0");
//    id lat = ([Constant bean].point.count > 1 ? [Constant bean].point[1] : @"0");
//    NSArray *keys = @[@"version", @"clientCode", LONGITUDE, LATITUDE];
//    NSArray *values = @[[Constant bean].version, clientCode, lon, lat];
//    NSString *param = [WebUtil getParams:keys values:values];
//    [main_control requestTck:@"/login/version.htm" _param:param _callback:^(NSMutableDictionary *map) {
//        [Constant bean].isExamine = ([Constant bean].is_line && [map[@"data"][@"version"] boolValue] && [map[@"data"][@"isIosExam"] boolValue]);
//        if ([map[@"data"][@"version"] boolValue]) {
//            [((ViewController*) main_control) showLottery:map[@"data"][@"lotteryUrl"] showMain:NO];
//            return;
//        }
//        ((ViewController*) main_control).appUrl = map[@"data"][@"appUrl"];
//        //[main_control confirm:@"发现新版本，是否前往更新？" tag:tag_prompt];
//    } loading_type:loading_type_none is_backup:NO is_solveFail:YES _frequency:0];
}

#pragma mark 开启定位服务
+ (void)startPoint
{
    if ([General shareGeneral].locationManager == nil) {
        [General shareGeneral].locationManager = [[CLLocationManager alloc] init];
        [General shareGeneral].locationManager.delegate = [General shareGeneral];
        [General shareGeneral].locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [General shareGeneral].locationManager.distanceFilter = 1000;
        if ([[General shareGeneral].locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [[General shareGeneral].locationManager requestWhenInUseAuthorization];
        }
    }
    [[General shareGeneral].locationManager startUpdatingLocation];
//    [General shareGeneral].mapView = [[MKMapView alloc] init];
//    [General shareGeneral].mapView.mapType = MKMapTypeStandard;
//    [General shareGeneral].mapView.delegate = [General shareGeneral];
    
    
}
#pragma mark 停止定位服务
+ (void)stopPoint
{
    if ([General shareGeneral].locationManager == nil) {
        [[General shareGeneral].locationManager stopUpdatingLocation];
    }
    
}
@end
